package GUI.studentGUI;

import java.awt.Component;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import common.Perror;

import GUI.MainGUI;
import controllers.StudentController;

//import javax.swing.border.TitledBorder;

@SuppressWarnings("serial")
public class StudentExam extends JPanel implements ActionListener,
		FocusListener, KeyListener {

	private JButton btnStartExam;

	private JButton btnExit;
	private JButton btnMainMenu;

	private JLabel lblExamType;
	private JLabel label;
	private JLabel lblExamCode;

	private JTextField txtEnterCode;

	private JRadioButton rdbtnComputerizedExam;
	private JRadioButton rdbtnManualExam;

	private StudentController StudentCtrl;
	private JButton btnUpLoad;
	private JTextField txtEnterId;

	public StudentExam(StudentController StudentCtrl) {
		super();
		this.StudentCtrl = StudentCtrl;
		initialize();
	}

	private void initialize() {
		setBorder(new TitledBorder(null, " exam: ", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));
		setBackground(SystemColor.activeCaption);
		setBounds(0, 0, MainGUI.setX, MainGUI.setY);

		setLayout(null);

		add(getMainMenu());
		add(getExit());
		add(getEnterCode());
		add(getStartExam());
		add(getExamCode());
		add(getrdbtnManualExam());
		add(getrdbtnComputerizedExam());
		add(getLblExamType());
		add(getLabel());
		add(getbtnUpLoad());

		JLabel lblEnterId = new JLabel("Enter ID:");
		lblEnterId.setFont(new Font("Tahoma", Font.ITALIC, 14));
		lblEnterId.setBounds(238, 66, 93, 14);
		add(lblEnterId);
		add(GETtxtEnterId());
		groupButten();
		rdbtnManualExam.setSelected(true);
		// add();
	}

	private JTextField GETtxtEnterId() {
		if (txtEnterId == null) {
			txtEnterId = new JTextField();
			txtEnterId.setText("Enter ID");
			txtEnterId.setColumns(10);
			txtEnterId.addKeyListener(this);
			txtEnterId.addFocusListener(this);
			txtEnterId.setBounds(229, 93, 124, 20);
		}
		return txtEnterId;
	}

	private JRadioButton getrdbtnManualExam() {

		if (rdbtnManualExam == null) {

			rdbtnManualExam = new JRadioButton("Manual exam");
			rdbtnManualExam.setBounds(21, 171, 173, 23);
			rdbtnManualExam.addActionListener(this);
		}
		return rdbtnManualExam;
	}

	private JRadioButton getrdbtnComputerizedExam() {
		if (rdbtnComputerizedExam == null) {

			rdbtnComputerizedExam = new JRadioButton("Computerized Exam", true);
			rdbtnComputerizedExam.setBounds(23, 213, 171, 23);
		}
		return rdbtnComputerizedExam;
	}

	private JTextField getEnterCode() {
		if (txtEnterCode == null) {
			txtEnterCode = new JTextField();
			txtEnterCode.setText("Enter exam code");
			txtEnterCode.setBounds(21, 93, 124, 20);
			txtEnterCode.setColumns(10);
			txtEnterCode.addFocusListener(this);
			txtEnterCode.addKeyListener(this);
		}
		return txtEnterCode;
	}

	private JButton getMainMenu() {
		if (btnMainMenu == null) {
			btnMainMenu = new JButton("Main menu");
			btnMainMenu.setBounds(21, 432, 124, 23);
			btnMainMenu.addActionListener(this);
		}
		return btnMainMenu;
	}

	private JButton getbtnUpLoad() {
		if (btnUpLoad == null) {
			btnUpLoad = new JButton("UPLOAD EXAM");
			btnUpLoad.setBounds(282, 213, 146, 23);
			btnUpLoad.setEnabled(false);
			btnUpLoad.addActionListener(this);

			//
		}
		return btnUpLoad;
	}

	private JButton getExit() {
		if (btnExit == null) {
			btnExit = new JButton("Exit");
			btnExit.setBounds(343, 432, 115, 23);
			btnExit.addActionListener(this);
		}
		return btnExit;
	}

	private JButton getStartExam() {
		if (btnStartExam == null) {
			btnStartExam = new JButton("start exam");
			btnStartExam.setBounds(169, 432, 146, 23);
			btnStartExam.setEnabled(false);
			btnStartExam.addActionListener(this);
		}
		return btnStartExam;
	}

	private JLabel getExamCode() {
		if (lblExamCode == null) {
			lblExamCode = new JLabel("exam code:");
			lblExamCode.setFont(new Font("Tahoma", Font.ITALIC, 14));
			lblExamCode.setBounds(21, 55, 227, 33);

		}
		return lblExamCode;

	}

	private JLabel getLblExamType() {
		if (lblExamType == null) {
			lblExamType = new JLabel("exam type:");
			lblExamType.setBounds(23, 137, 107, 14);
		}
		return lblExamType;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("press when you ready to start");
			label.setFont(new Font("Tahoma", Font.BOLD, 11));
			label.setBounds(147, 407, 184, 14);
		}
		return label;
	}

	private void groupButten() {
		ButtonGroup Grop1 = new ButtonGroup();
		Grop1.add(rdbtnComputerizedExam);
		Grop1.add(rdbtnManualExam);
		// Grop1.add();

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnExit) { // logout
			StudentCtrl.handleStudentGUI(StudentController.EXIT);
			
		}
		if (e.getSource() == btnMainMenu) { // return to user main screen
			StudentCtrl.handleStudentGUI(StudentController.MAIN);
			
		}
		if (e.getSource() == btnStartExam) {
			if (rdbtnComputerizedExam.isSelected()) {
				StudentCtrl.startComputerizedExam(txtEnterCode.getText(),
						txtEnterId.getText());

			}
			if (rdbtnManualExam.isSelected()) {
				StudentCtrl.getFile(txtEnterCode.getText());
				btnUpLoad.setEnabled(true);
			}

			// StudentCtrl.handleStudentGUI(StudentController.DOWNLOAD_MANUAL_EXAM);
			// mainGUI.handleStudentGUI(6); //put time screen

		}
		if (e.getSource() == btnUpLoad) {
			btnUpLoad.setEnabled(false);
			lock(true);
			StudentCtrl.upLoad(txtEnterCode.getText(),
					Integer.parseInt(txtEnterId.getText()));

			// StudentCtrl.handleStudentGUI(5);
		}

		if (e.getSource() == rdbtnManualExam) {
			rdbtnComputerizedExam.setSelected(false);
			rdbtnManualExam.setSelected(true);

		}

		if (e.getSource() == rdbtnManualExam) {
			rdbtnComputerizedExam.setSelected(false);
			rdbtnManualExam.setSelected(true);
		}

		if (e.getSource() == rdbtnComputerizedExam) {

			rdbtnComputerizedExam.setSelected(true);
			rdbtnManualExam.setSelected(false);

		}

	}

	public String getStudentID() {
		return txtEnterId.getText();
	}

	public void lock(Boolean b) {
		btnStartExam.setEnabled(b);
		txtEnterCode.setEditable(b);
		txtEnterId.setEnabled(b);
		btnExit.setEnabled(b);
		btnMainMenu.setEnabled(b);

	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		

	}

	@Override
	public void keyReleased(KeyEvent e) {
		if (e.getSource() == txtEnterCode) {
			if (e.isActionKey() || e.getKeyChar() == '\n')
				return;
			if (txtEnterCode.getText().length() == 4) {
				int counter = 0;
				for (int i = 0; i < 4; i++)
					if (Character.isDigit(txtEnterCode.getText().charAt(i)))
						counter++;
				if ((counter == 0) || (counter == 4)) {
					Perror.pError("Code must contains at least 1 digit and 1 character.");
					btnStartExam.setEnabled(false);
				} else
					btnStartExam.setEnabled(true);
			} else if (txtEnterCode.getText().length() > 4) {
				Perror.pError("Code must be 4 characters.");
				btnStartExam.setEnabled(false);
			} else {
				btnStartExam.setEnabled(false);
			}
		}
		if (e.getSource() == txtEnterId)
		{
			if (!Character.isDigit(e.getKeyChar()))
				txtEnterId.setText("");
					
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		

	}

	@Override
	public void focusGained(FocusEvent e) {
		if (e.getSource() == txtEnterCode)
			if (txtEnterCode.getText().equals("Enter exam code"))
				txtEnterCode.setText("");
		if(e.getSource() == txtEnterId)	
			if (txtEnterId.getText().equals("Enter ID"))
				txtEnterId.setText("");
			

	}

	@Override
	public void focusLost(FocusEvent e) {
		if (e.getSource() == txtEnterCode) 
			if (txtEnterCode.getText().equals(""))
				txtEnterCode.setText("Enter exam code");
		
		if(e.getSource() == txtEnterId)	
			if (txtEnterId.getText().equals(""))
				txtEnterId.setText("Enter ID");
				
	}

	public void clearAll() {
		txtEnterId.setText("Enter ID");
		txtEnterCode.setText("Enter exam code");
		
	}
}
